﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class UpdatePanel : OptionBasePanel
{
    private Ray ray;
    private RaycastHit2D[] hit;
    public BattleObject tower;
    public Block blk;
    public Text upText, downText, rightText, leftText;
    private bool activeUp,activeDown,activeRight,activeLeft;
    private float repairAP;
    public override void OnEnter()
    {
        base.OnEnter();
        setUpdate();
    }

    private void setUpdate()
    {
        if (tower.gameObject.name.Contains("Production"))
        {
            activeUp = true;
            activeDown = true;
            activeRight = false;
            activeLeft = false;
            upText.text = "修复";
            downText.text = "拆除";
            rightText.text = "";
            leftText.text = "";
        }
        else if(tower.gameObject.name.Contains("Nut"))
        {
            if (tower.GetLevel() == 0 || tower.GetLevel() == 1)
            {
                activeUp = true;
                activeDown = true;
                activeRight = true;
                activeLeft = false;
                upText.text = "修复";
                downText.text = "拆除";
                rightText.text = "升级";
                leftText.text = "";
                right1.text = "AP:" + CostOfLevelUp.CostAPForLevelUp[tower.GetLevel()];
                right2.text = "G:" + CostOfLevelUp.CostForLevelUp[tower.GetLevel()];
            }
            else
            {
                activeUp = true;
                activeDown = true;
                activeRight = false;
                activeLeft = false;
                upText.text = "修复";
                downText.text = "拆除";
                rightText.text = "";
                leftText.text = "";
            }
        }
        else
        {
            if (tower.GetLevel() == 0 || tower.GetLevel() == 1)
            {
                activeUp = true;
                activeDown = true;
                activeRight = true;
                activeLeft = false;
                upText.text = "修复";
                downText.text = "拆除";
                rightText.text = "升级";
                leftText.text = "";
                right1.text = "AP:" + CostOfLevelUp.CostAPForLevelUp[tower.GetLevel()];
                right2.text = "G:" + CostOfLevelUp.CostForLevelUp[tower.GetLevel()];
            }
            else if(tower.gameObject.name.Contains("Crossbow"))
            {
                if (!tower.gameObject.name.Contains("Base"))
                {
                    activeUp = true;
                    activeDown = true;
                    activeRight = false;
                    activeLeft = false;
                    upText.text = "修复";
                    downText.text = "拆除";
                    rightText.text = "";
                    leftText.text = "";
                }
                else
                {
                    activeUp = true;
                    activeDown = true;
                    activeRight = true;
                    activeLeft = true;
                    upText.text = "修复";
                    downText.text = "拆除";
                    rightText.text = "连弩";
                    leftText.text = "重弩";
                    right1.text = "AP:" + BuildingManager.Instance.GetRequiredAP(BuildingType.WhiteAngelCrossbow);
                    right2.text = "G:" + BuildingManager.Instance.GetRequiredCost(BuildingType.WhiteAngelCrossbow);
                    left1.text = "AP:" + BuildingManager.Instance.GetRequiredAP(BuildingType.DarkFrameCrossbow);
                    left2.text = "G:" + BuildingManager.Instance.GetRequiredCost(BuildingType.DarkFrameCrossbow);
                }
            }
            else if(tower.gameObject.name.Contains("Cannon"))
            {
                if (!tower.gameObject.name.Contains("Base"))
                {
                    activeUp = true;
                    activeDown = true;
                    activeRight = false;
                    activeLeft = false;
                    upText.text = "修复";
                    downText.text = "拆除";
                    rightText.text = "";
                    leftText.text = "";
                }
                else
                {
                    activeUp = true;
                    activeDown = true;
                    activeRight = true;
                    activeLeft = true;
                    upText.text = "修复";
                    downText.text = "拆除";
                    rightText.text = "灵石";
                    leftText.text = "火石";
                    left1.text = "AP:" + BuildingManager.Instance.GetRequiredAP(BuildingType.FireAngelCannon);
                    left2.text = "G:" + BuildingManager.Instance.GetRequiredCost(BuildingType.FireAngelCannon);
                    right1.text = "AP:" + BuildingManager.Instance.GetRequiredAP(BuildingType.SoulCannon);
                    right2.text = "G:" + BuildingManager.Instance.GetRequiredCost(BuildingType.SoulCannon);
                }
            }
        }
    }

    protected override void DoLeftOption()
    {

        if(activeLeft)
        {
            if(tower.gameObject.name.Contains("Crossbow"))
            {
                if (!upgradeTower(BuildingType.DarkFrameCrossbow))
                    return;
                UIManager.Instance.PopPanel();
            }
            else if (tower.gameObject.name.Contains("Cannon"))
            {
                if (!upgradeTower(BuildingType.FireAngelCannon))
                    return;
                UIManager.Instance.PopPanel();
            }
        }
    }

    protected override void DoRightOption()
    {
        if(activeRight)
        {
            if (tower.GetLevel() == 2)
            {
                if (tower.gameObject.name.Contains("Crossbow"))
                {
                    if (!upgradeTower(BuildingType.WhiteAngelCrossbow))
                        return;
                    UIManager.Instance.PopPanel();
                }
                else if (tower.gameObject.name.Contains("Cannon"))
                {
                    if (!upgradeTower(BuildingType.SoulCannon))
                        return;
                    UIManager.Instance.PopPanel();
                }
            }
            else
            {
                if (!APController.Instance.CheckDecreaseAPForBreakBlock(CostOfLevelUp.CostAPForLevelUp[tower.GetLevel()]) || !ResourceManager.Instance.CheckResource(ResourceType.Money, CostOfLevelUp.CostAPForLevelUp[tower.GetLevel()]))
                {
                    //UIManager.Instance.PopPanel();
                    return;
                }
                APController.Instance.TryDecreaseAPForBreakBlock(CostOfLevelUp.CostAPForLevelUp[tower.GetLevel()]);
                ResourceManager.Instance.TryConsumeResource(ResourceType.Money, CostOfLevelUp.CostAPForLevelUp[tower.GetLevel()]);
                EffectObjectManager.Instance.TowerUpgradeEf0fects();
                tower.LevelUp();
                UIManager.Instance.PopPanel();
            }
        }
    }

    protected override void DoUpOption()//修复
    {
        base.DoUpOption();
        if(activeUp)
        {
            if (!APController.Instance.CheckDecreaseAPForBreakBlock(repairAP))
            {
                //UIManager.Instance.PopPanel();
                return;
            }
            APController.Instance.TryDecreaseAPForBreakBlock(repairAP);
            EffectObjectManager.Instance.TowerUpgradeEf0fects();
            tower.repair();
            UIManager.Instance.PopPanel();
        }
    }

    protected override void DoDownOption()
    {
        base.DoDownOption();
        if(activeDown)
        {
            EffectObjectManager.Instance.TowerSellEffects();
            blk.Dismantle();
            quit();
        }
    }

    bool upgradeTower(BuildingType type)
    {
        if (!APController.Instance.CheckDecreaseAPForBuilding(type) || !ResourceManager.Instance.CheckResourceForBuilding(type))
        {
            //UIManager.Instance.PopPanel();
            return false;
        }
        APController.Instance.TryDecreaseAPForBuilding(type);
        ResourceManager.Instance.TryConsumeResourceForBuilding(type);
        EffectObjectManager.Instance.TowerUpgradeEf0fects();
        blk.Init(type);
        return true;
    }

    protected override void Update()
    {
        if(tower==null)
        {
            return;
        }
        if (tower.gameObject.name.Contains("Production"))
        {
            repairAP = (float)(BuildingManager.Instance.GetRequiredAP(BuildingType.Production) / 2) * (1 - (tower.getHP() / tower.getMaxHP()));
        }
        else if (tower.gameObject.name.Contains("Nut"))
        {
            repairAP = (float)(BuildingManager.Instance.GetRequiredAP(BuildingType.Nut) / 2) * (1 - (tower.getHP() / tower.getMaxHP()));
        }
        else
        {
            if (tower.gameObject.name.Contains("Crossbow"))
            {
                if (!tower.gameObject.name.Contains("Base"))
                {
                    if (tower.gameObject.name.Contains("DarkFrame"))
                    {
                        repairAP = (float)(BuildingManager.Instance.GetRequiredAP(BuildingType.DarkFrameCrossbow) / 2) * (1 - (tower.getHP() / tower.getMaxHP()));
                    }
                    else if (tower.gameObject.name.Contains("WhiteAngle"))
                    {
                        repairAP = (float)(BuildingManager.Instance.GetRequiredAP(BuildingType.WhiteAngelCrossbow) / 2) * (1 - (tower.getHP() / tower.getMaxHP()));
                    }
                }
                else
                {
                    repairAP = (float)(BuildingManager.Instance.GetRequiredAP(BuildingType.BaseCrossbow) / 2) * (1 - (tower.getHP() / tower.getMaxHP()));
                }
            }
            else if (tower.gameObject.name.Contains("Cannon"))
            {
                if (!tower.gameObject.name.Contains("Base"))
                {
                    if (tower.gameObject.name.Contains("FireAngel"))
                    {
                        repairAP = (float)(BuildingManager.Instance.GetRequiredAP(BuildingType.FireAngelCannon) / 2) * (1 - (tower.getHP() / tower.getMaxHP()));
                    }
                    else if (tower.gameObject.name.Contains("Soul"))
                    {
                        repairAP = (float)(BuildingManager.Instance.GetRequiredAP(BuildingType.SoulCannon) / 2) * (1 - (tower.getHP() / tower.getMaxHP()));
                    }
                }
                else
                {
                    repairAP = (float)(BuildingManager.Instance.GetRequiredAP(BuildingType.BaseCannon) / 2) * (1 - (tower.getHP() / tower.getMaxHP()));
                }
            }
        }
        up1.text = "AP:" + System.Math.Round(repairAP);
        base.Update();
    }
}
